#!/bin/bash
#apt update
#apt install gcc gdb
####################################################################
##change apt source to cn
#disable warngin:tty

echo 'tty -s && mesg n || true' > ~/.profile

cat >> ~/.bashrc << EOF
if [ -f ~/.profile ] ; then 
    . ~/.profile 
fi
EOF
#sed -i 's/mesg/tty -s \&\& mesg/g' ~/.profile

#apt-get update 
#apt-get install -y apt-utils

#deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
#deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
#deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
#deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
#deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
#deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

change_source_x86()
{
cat > /etc/apt/sources.list << EOF
    deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
}

change_source_arm64()
{
    cat > /etc/apt/sources.list << EOF

    deb http://ports.ubuntu.com/ bionic main restricted universe multiverse
    deb-src http://ports.ubuntu.com/ bionic main restricted universe multiverse
    deb http://ports.ubuntu.com/ bionic-updates main restricted universe multiverse
    deb-src http://ports.ubuntu.com/ bionic-updates main restricted universe multiverse
    deb http://ports.ubuntu.com/ bionic-security main restricted universe multiverse
    deb-src http://ports.ubuntu.com/ bionic-security main restricted universe multiverse
    deb http://ports.ubuntu.com/ bionic-backports main restricted universe multiverse
    deb-src http://ports.ubuntu.com/ bionic-backports main restricted universe multiverse
    deb http://ports.ubuntu.com/ubuntu-ports/ bionic main universe restricted
    deb-src http://ports.ubuntu.com/ubuntu-ports/ bionic main universe restricted

EOF
}

if uname -a | grep aarch64 >> /dev/null ; then
    change_source_arm64
else
    change_source_x86
fi

#apt-get update 
#apt-get upgrade -y

apt update 
apt install -y apt-utils
apt upgrade -y

#apt install -y wget curl  sudo  \
#	openssh-client openssh-server  openssh-sftp-server vim net-tools

#apt-get install -y wget curl psmisc build-essential autotools-dev sudo  \
apt install -y tree wget curl psmisc build-essential autotools-dev sudo  \
    xz-utils dialog net-tools netcat tcpdump ethtool nload  traceroute tcptraceroute iproute2 \
    libicu-dev autoconf automake libtool \
    vim git sed  rsync \
    gcc g++ gdb  \
    clang-4.0 clang-tools-4.0 python-lldb-4.0  liblldb-4.0-dev \
    libclang-4.0-dev lldb-4.0 liblldb-4.0  \
    cmake doxygen libbz2-dev libssl-dev libgmp3-dev libpython2.7-dev python2.7-dev graphviz \
    python3 python3-pip python3-dev \
    inetutils-ping inetutils-telnet inetutils-ftp \
    openssh-client openssh-server  openssh-sftp-server  nginx 

apt install -y node-gyp nodejs npm

npm install -g npm@latest-6

npm install pm2 -g
npm install http-server -g


echo "root:macvi" | chpasswd
sed -i "s/^.*#UsePrivilegeSeparation.*$/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
sed -i "s/^.*UsePAM.*$/UsePAM no/g" /etc/ssh/sshd_config

sed -i "s/^.*#PermitRootLogin.*$/PermitRootLogin yes/g" /etc/ssh/sshd_config
sed -i "s/^.*#TCPKeepAlive.*$/TCPKeepAlive yes/g" /etc/ssh/sshd_config
sed -i "s/^.*#ClientAliveInterval.*$/ClientAliveInterval 30/g" /etc/ssh/sshd_config
sed -i "s/^.*#ClientAliveCountMax.*$/ClientAliveCountMax 60/g" /etc/ssh/sshd_config
mkdir -p /var/run/sshd
mkdir -p /var/run/log
mkdir -p ~/.ssh
chmod 700 ~/.ssh

apt clean
apt autoclean
rm -rf /var/lib/apt/lists/*

git config --global core.editor vim
git config --global user.name "vi"
git config --global user.email "vi@docker.com"

mkdir -p ~/run/log
mkdir -p ~/run/bin
mkdir -p ~/git/log
mkdir ~/git/tmp
mkdir ~/tmp

cat >> ~/run/bin/start_service.sh << EOF
#!/usr/bin/env bash

log_file=~/run/log/start_service.log
echo "[`date +%Y-%m-%d_%H:%M:%S`] start sshd service" >> $log_file
pidof sshd >/dev/null || /usr/sbin/sshd -D &

EOF
chmod +x ~/run/bin/start_service.sh 

#cd ~/git && git clone git://git.igcc.cc/tool_sh.git && cd tool_sh && ./install.sh
#cd ~/git && git clone git://git.igcc.cc/p2pcpp.git && cd p2pcpp/shell && ./install_dependence.sh
#cd ~/git && git clone git://git.igcc.cc/chain.git 
#cd ~/git && git clone git://git.igcc.cc/scf-core.git && cd scf-core && git branch --track vi origin/vi && git checkout vi && ./ultrainio_build_cn.sh

#rm -rf ~/git
[ -f ~/install_base.sh ] && rm  ~/install_base.sh
pwd
echo "-- info, image_base build succeed."

exit 0



